package org.dahg.project.controller.model.beans;

import javax.interceptor.Interceptors;
import javax.persistence.EntityManager;
import javax.persistence.Query;
import org.dahg.project.controller.model.exceptions.ControllerException;
import org.dahg.project.controller.model.services.Service;
import org.dahg.project.controller.persist.SpringPersistence;
import org.dahg.project.controller.util.Encrypt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.ejb.interceptor.SpringBeanAutowiringInterceptor;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

/**
 *
 * @author dahg
 */
@Interceptors(SpringBeanAutowiringInterceptor.class)
public abstract class AbstractBean<T> implements Service<T> {

	@Autowired
	@Qualifier("persistence")
	private SpringPersistence persistence;
	
	@Autowired
	@Qualifier("encrypt")
	private Encrypt decrypt;
	
	protected EntityManager getEntityManager() {
		return persistence.getEntityManager();
	}
	
	protected abstract Class<T> getClazz();

	@Override
	public T getById(Object id) {
		return getEntityManager().find(getClazz(), id);
	}

	@Override
	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
	public void persist(T obj) throws ControllerException {
		try {
			getEntityManager().persist(obj);
		}
		catch (Exception e) {
			throw new ControllerException(e);
		}
	}

	@Override
	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
	public void merge(T obj) {
		getEntityManager().merge(obj);
	}
	
	@Transactional(readOnly=false,propagation=Propagation.REQUIRES_NEW)
	protected void remove(String sql,Object primaryKey) {
		Query q=getEntityManager().createQuery(sql).setParameter("id", primaryKey);
		q.executeUpdate();
	}
	
	public Encrypt getDecrypt() {
		return decrypt;
	}
}
